﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MDF
{
    class DictionaryInfo
    {
        //xml文件读写
        private IO_xml m_xml;
        /// <summary>
        /// 字典信息类初始化
        /// </summary>
        public DictionaryInfo()
        {
            //实例化xml对象
            m_xml = new IO_xml("ShuXingZiDian");
        }

        /// <summary>
        /// 获取属性语义树
        /// </summary>
        /// <returns></returns>
        public TreeNode[] GetTreeNodesInfo()
        {
            try
            {
                //创建节点名称数组
                string[] infoNames = new string[] { "管点", "管线" };
                //创建节点列表
                List<TreeNode> treeNodes = new List<TreeNode>();
                //遍历构建节点
                foreach (string infoName in infoNames)
                {
                    //创建管线节点
                    TreeNode infoNode = new TreeNode(infoName);
                    //获取管线字段节点名称,并遍历
                    foreach (string name in m_xml.ReadChildNodesName(infoName))
                    {
                        //创建字段节点
                        TreeNode node = new TreeNode(name);
                        //查询是否是包含同义词的字段
                        if (m_xml.ReadValueByKey(name, "IsSame") == "True")
                        {
                            node.Checked = true;
                        }
                        //查询是否存在子节点，并遍历获取
                        foreach (string childName in m_xml.ReadChildNodesName(name))
                        {
                            //创建字段节点
                            TreeNode childNode = new TreeNode(childName);
                            //查询是否是包含同义词的属性值
                            if (m_xml.ReadValueByKey(childName, "IsSame") == "True")
                            {
                                childNode.Checked = true;
                            }
                            //添加到父节点
                            node.Nodes.Add(childNode);
                        }
                        //添加到根节点
                        infoNode.Nodes.Add(node);
                    }
                    //添加到列表
                    treeNodes.Add(infoNode);
                }
                //返回结果
                return treeNodes.ToArray();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 获取同义词集
        /// </summary>
        /// <returns></returns>
        public string GetSynSetInfo(string name,string check)
        {
            try
            {
                string synSet = "无同义词信息";
                if (check == "True")
                {
                    
                    foreach (string SynName in m_xml.ReadChildNodesName("SynSet"))
                    {
                        if (m_xml.ReadInnerText(SynName).Contains(name))
                        {
                            //找到同义词集后退出
                            synSet = m_xml.ReadInnerText(SynName).Replace('_',' ');
                            break;
                        }
                        
                    }
                }
                return synSet;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 修改字典信息
        /// </summary>
        /// <param name="oldNodeName">旧的节点名</param>
        /// <param name="newNodeName">修改的新的节点名</param>
        /// <param name="check">节点是否选中</param>
        /// <returns>修改成功返回ture，失败返回false</returns>
        public bool ReviseDicNodeInfo(string oldNodeName, string newNodeName, string check)
        {
            try
            {
                //保存
                if(m_xml.ReviseXml(oldNodeName, newNodeName, "IsSame", check))
                {
                    //成功
                    return true;
                }
                else
                {
                    //失败
                    return false;
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 修改字典信息
        /// </summary>
        /// <param name="nodeName">树节点名称</param>
        /// <param name="synInfo">同义词集信息</param>
        public void ReviseDicSynInfo(string nodeName, string synInfo)
        {
            try
            {
                //保存同义词集
                foreach (string SynName in m_xml.ReadChildNodesName("SynSet"))
                {
                    if (m_xml.ReadInnerText(SynName).Contains(nodeName))
                    {
                        //更新同义词集
                        m_xml.WriteXml("SynSet", SynName, synInfo);
                        break;
                    }

                }
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
